package com.pure.common.recursion;

/**
 * @description: 二叉树深度遍历
 * @date: 2022-08-30 11:13
 * @since 1.2.0
 **/
public class DepthUtil {

    // 结点类
    public static class TreeNode {

        private int node;
        private TreeNode left;
        private TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int node) {
            this.node = node;
        }

        public int getNode() {
            return node;
        }

        public void setNode(int node) {
            this.node = node;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "node=" + node +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    public static void main(String[] args) {

        TreeNode root$1 = new TreeNode(1);
        TreeNode node$2 = new TreeNode(2);
        TreeNode node$3 = new TreeNode(3);
        TreeNode node$4 = new TreeNode(4);
        TreeNode node$5 = new TreeNode(5);
        TreeNode node$6 = new TreeNode(6);

        // 1 结点
        root$1.setLeft(node$2);
        root$1.setRight(node$3);

        // 2 结点
        node$2.setLeft(node$4);
        node$2.setRight(node$5);

        node$5.setLeft(node$6);

        System.out.println("root 结点深度是：" + depth(root$1));
        System.out.println("node$2 结点深度是：" + depth(node$2));
        System.out.println("node$3 结点深度是：" + depth(node$3));
        System.out.println("node$4 结点深度是：" + depth(node$4));
        System.out.println("node$5 结点深度是：" + depth(node$5));
    }


    // 深度递归函数
    public static int depth(TreeNode root) {
        if (null == root) {
            return 0;
        }
        int l, r;
        l = depth(root.getLeft());
        r = depth(root.getRight());
        return Math.max(l, r) + 1;
    }

}
